import { ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { Modal } from 'ant-design-vue'
import type { TablePaginationConfig } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue/es/form'
import type { Config } from '@/types/api/config/config'
import type { ConfigGroup } from '@/types/api/config/configGroup'
import { useSettingStore } from '@/stores/config/setting'
import { storeToRefs } from 'pinia'

/**
 * 系统配置管理业务逻辑钩子函数
 */
export default function useSettingManagement() {
  // ------------ 国际化和工具引入 ------------
  const { t } = useI18n()
  
  // ------------ 使用状态管理 ------------
  const store = useSettingStore()
  
  // 从store中提取响应式状态
  const {
    // 基础状态
    loading,
    submitLoading,
    
    // 数据列表
    groupList,
    configList,
    
    // 查询参数
    queryParams,
    
    // 分页配置
    pagination,
    
    // 弹窗状态
    groupModalVisible,
    configModalVisible,
    
    // 表单状态
    groupFormMode,
    configFormMode,
    currentGroupId,
    currentGroupName,
    groupFormData,
    configFormData
  } = storeToRefs(store)

  // ------------ 组件引用 ------------
  const groupFormRef = ref<FormInstance>()
  const configFormRef = ref<FormInstance>()

  // ------------ 初始化方法 ------------
  // 页面加载时获取数据
  onMounted(async () => {
    try {
      await store.fetchGroupList()
    } catch (error) {
      Modal.error({
        title: t('common.error'),
        content: t('setting.loadFailed')
      })
    }
  })

  // ------------ 分组操作方法 ------------
  /**
   * 分组选择
   * @param groupId 分组ID
   */
  const handleGroupSelect = (groupId: string) => {
    store.currentGroupId = groupId
    store.fetchConfigList()
  }

  /**
   * 分组添加
   */
  const handleGroupAdd = () => {
    groupFormMode.value = 'add'
    store.resetGroupForm()
    groupModalVisible.value = true
  }

  /**
   * 分组编辑
   * @param group 分组信息
   */
  const handleGroupEdit = (group: ConfigGroup) => {
    groupFormMode.value = 'edit'
    Object.assign(groupFormData.value, group)
    groupModalVisible.value = true
  }

  /**
   * 分组删除
   * @param group 分组信息
   */
  const handleGroupDelete = (group: ConfigGroup) => {
    Modal.confirm({
      title: t('common.confirm'),
      content: t('setting.deleteGroupConfirm'),
      okText: t('common.confirm'),
      okType: 'danger',
      cancelText: t('common.cancel'),
      async onOk() {
        await store.deleteGroup(group.id)
      }
    })
  }

  /**
   * 分组表单提交
   */
  const handleGroupSubmit = () => {
    if (groupFormMode.value === 'add') {
      store.createGroup()
    } else {
      store.updateGroup()
    }
  }

  // ------------ 配置操作方法 ------------
  /**
   * 配置添加
   */
  const handleConfigAdd = () => {
    configFormMode.value = 'add'
    store.resetConfigForm()
    configFormData.value.groupId = currentGroupId.value
    configModalVisible.value = true
  }

  /**
   * 配置编辑
   * @param record 配置信息
   */
  const handleConfigEdit = (record: Config) => {
    configFormMode.value = 'edit'
    Object.assign(configFormData.value, record)
    configModalVisible.value = true
  }

  /**
   * 配置删除
   * @param record 配置信息
   */
  const handleConfigDelete = (record: Config) => {
    Modal.confirm({
      title: t('common.confirm'),
      content: t('setting.deleteConfigConfirm'),
      okText: t('common.confirm'),
      okType: 'danger',
      cancelText: t('common.cancel'),
      async onOk() {
        await store.deleteConfig(record.id)
      }
    })
  }

  /**
   * 配置表单提交
   */
  const handleConfigSubmit = () => {
    if (configFormMode.value === 'add') {
      store.createConfig()
    } else {
      store.updateConfig()
    }
  }

  // ------------ 通用操作方法 ------------
  /**
   * 搜索
   */
  const handleSearch = () => {
    pagination.value.current = 1
    store.fetchConfigList()
  }

  /**
   * 重置搜索
   */
  const handleReset = () => {
    Object.assign(queryParams.value, {
      configName: '',
      configKey: ''
    })
    handleSearch()
  }

  /**
   * 刷新
   */
  const handleRefresh = () => {
    store.fetchConfigList()
  }

  /**
   * 表格分页变更
   * @param pag 分页信息
   */
  const handleTableChange = (pag: TablePaginationConfig) => {
    store.handlePageChange(pag.current || 1, pag.pageSize)
  }

  // 返回所有方法和状态
  return {
    // 状态
    loading,
    submitLoading,
    groupList,
    configList,
    queryParams,
    pagination,
    groupModalVisible,
    configModalVisible,
    groupFormMode,
    configFormMode,
    currentGroupId,
    currentGroupName,
    groupFormData,
    configFormData,
    
    // 分组方法
    handleGroupSelect,
    handleGroupAdd,
    handleGroupEdit,
    handleGroupDelete,
    handleGroupSubmit,
    
    // 配置方法
    handleConfigAdd,
    handleConfigEdit,
    handleConfigDelete,
    handleConfigSubmit,
    
    // 通用方法
    handleSearch,
    handleReset,
    handleRefresh,
    handleTableChange
  }
} 